﻿using Aliyun.OSS;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 相机
    /// </summary>
    [AppService(ServiceType = typeof(ICameraDetectionService), ServiceLifetime = LifeTime.Transient)]
    public class CameraDetectionService : BaseService<CameraDetectionEntity>, ICameraDetectionService
    {
        public PagedInfo<CameraDetectionDto> GetList(CameraDetectionQueryDto param)
        {
            var predicate = QueryExp(param);

            var response = Queryable()
                .OrderByDescending(x => x.CreateTime)
                .Where(predicate.ToExpression())
                .ToPage<CameraDetectionEntity, CameraDetectionDto>(param);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CameraDetectionEntity> QueryExp(CameraDetectionQueryDto parm)
        {
            var predicate = Expressionable.Create<CameraDetectionEntity>();
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.CameraName), it => it.CameraName.Contains(parm.CameraName));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.ProductId), it => it.ProductId.Contains(parm.ProductId));
            predicate = predicate.AndIF(parm.Result.IsNotEmpty(), it => it.Result == ((bool)parm.Result ? "OK" : "NG"));
            predicate = predicate.AndIF(parm.BeginAddTime != null, it => it.CreateTime >= parm.BeginAddTime);
            predicate = predicate.AndIF(parm.EndAddTime != null, it => it.CreateTime <= parm.EndAddTime);
            return predicate;
        }
    }
}